import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:yurun_smartmerchants/interface/DialogInterface.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/view/BaseDialog.dart';
import 'package:yurun_smartmerchants/interface/baosuncallback.dart';
import 'app_size.dart';

class DialogUtil {
  static final DialogUtil d = DialogUtil._internal();

  factory DialogUtil() => d;

  DialogUtil._internal();

  void show(BuildContext context, String titleContent, String confirm,
      bool isBarrierDismissible, DialogInterface dialogInterface) {
    showDialog(
        context: context,
        barrierDismissible: isBarrierDismissible,
        builder: (BuildContext context) {
          return DialogView(context, titleContent, confirm, dialogInterface);
        });
  }

  void showAnimation(
      BuildContext context,
      String titleContent,
      String confirm,
      int showAnimationTime,
      bool isBarrierDismissible,
      DialogInterface dialogInterface) {
    showGeneralDialog(
        context: context,
        pageBuilder: (context, anim1, anim2) {},
        barrierColor: Colors.black.withOpacity(.7),
        barrierDismissible: isBarrierDismissible,
        transitionDuration: Duration(milliseconds: showAnimationTime),
        transitionBuilder: (context, anim1, anim2, child) {
          return Transform.scale(
              scale: anim1.value,
              child:
                  DialogView(context, titleContent, confirm, dialogInterface));
        });
  }

  void showCustom(BuildContext context, bool isBarrierDismissible, Dialog dialog) {
    showDialog(
        context: context,
        barrierDismissible: isBarrierDismissible,
        builder: (BuildContext context) {
          return dialog;
        });
  }

  void showCustomAnimation(BuildContext context, String titleContent,
      int showAnimationTime, bool isBarrierDismissible, Dialog dialog) {
    showGeneralDialog(
        context: context,
        pageBuilder: (context, anim1, anim2) {},
        barrierColor: Colors.black.withOpacity(.7),
        barrierDismissible: isBarrierDismissible,
        transitionDuration: Duration(milliseconds: showAnimationTime),
        transitionBuilder: (context, anim1, anim2, child) {
          return Transform.scale(scale: anim1.value, child: dialog);
        });
  }

  Dialog DialogView(BuildContext context, String titleContent, String confirm,
      DialogInterface dialogInterface) {
    return BaseDialog(
        Column(
          children: <Widget>[
            Container(
              alignment: Alignment.center,
              child: Text(titleContent,
                  style: TextStyle(
                      fontSize: AppSize.sp(40), color: Colours.color_434343)),
              padding: EdgeInsets.only(top: AppSize.sp(86)),
            ),
            Spacer(),
            Container(
              color: Colours.color_dcdcdc,
              width: MediaQuery.of(context).size.width * .8,
              height: AppSize.height(1),
            ),
            Row(
              children: <Widget>[
                GestureDetector(
                  onTap: () {
                    Navigator.of(context).pop(this);
                  },
                  child: Container(
                      width: MediaQuery.of(context).size.width * .4,
                      height: AppSize.height(98),
                      child: Text(
                        "取消",
                        style: TextStyle(fontSize: AppSize.sp(30)),
                      ),
                      alignment: Alignment.center,
                      decoration: new BoxDecoration(
                          color: Colours.title_bg,
                          borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(AppSize.sp(18)),
                          ))),
                ),
                GestureDetector(
                  onTap: () {
                    Navigator.of(context).pop(this);
                    if (dialogInterface != null) dialogInterface.sumit();
                  },
                  child: Container(
                      width: MediaQuery.of(context).size.width * .4,
                      height: AppSize.height(98),
                      alignment: Alignment.center,
                      child: Text(
                        confirm,
                        style: TextStyle(
                            color: Colours.white, fontSize: AppSize.sp(30)),
                      ),
                      decoration: new BoxDecoration(
                          color: Colours.app_main,
                          borderRadius: BorderRadius.only(
                              bottomRight: Radius.circular(AppSize.sp(18))))),
                )
              ],
            ),
          ],
        ),
        AppSize.height(310),
        MediaQuery.of(context).size.width * .8);
  }

  void showBaoSunDialog(BuildContext context, String confirm,
      bool isBarrierDismissible, BaoSunCallback baoSunCallback) {
    showDialog(
        context: context,
        barrierDismissible: isBarrierDismissible,
        builder: (BuildContext context) {
          return BaoSunDialog(context, confirm, baoSunCallback);
        });
  }

  TextEditingController controller = TextEditingController();

  Dialog BaoSunDialog(BuildContext context,String confirm, BaoSunCallback baoSunCallback){
    return BaseDialog(
        Column(
          children: <Widget>[
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('报损数量',style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_434343),),
                  Container(
                    margin: EdgeInsets.only(left: AppSize.width(50)),
                    width: AppSize.width(200),
                    child: TextField(
                      maxLength: 10,
                      controller: controller,
                      style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_313131),
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        counterText: '',
                        border: InputBorder.none,
                        hintText: '输入报损数量',
                        hintStyle: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_abacb5),
                      ),
                    ),
                  )
                ],
              ),
              padding: EdgeInsets.only(top: AppSize.sp(86)),
            ),
            Spacer(),
            Container(
              color: Colours.color_dcdcdc,
              width: MediaQuery.of(context).size.width * .8,
              height: AppSize.height(1),
            ),
            Row(
              children: <Widget>[
                GestureDetector(
                  onTap: () {
                    controller.clear();
                    Navigator.of(context).pop(this);
                  },
                  child: Container(
                      width: MediaQuery.of(context).size.width * .4,
                      height: AppSize.height(98),
                      child: Text(
                        "取消",
                        style: TextStyle(fontSize: AppSize.sp(30)),
                      ),
                      alignment: Alignment.center,
                      decoration: new BoxDecoration(
                          color: Colours.title_bg,
                          borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(AppSize.sp(18)),
                          ))),
                ),
                GestureDetector(
                  onTap: () {
                    if (baoSunCallback != null){
                      baoSunCallback.callback(quantity: controller.text);
                      controller.clear();
                    }
                    Navigator.of(context).pop(this);
                  },
                  child: Container(
                      width: MediaQuery.of(context).size.width * .4,
                      height: AppSize.height(98),
                      alignment: Alignment.center,
                      child: Text(
                        confirm,
                        style: TextStyle(
                            color: Colours.white, fontSize: AppSize.sp(30)),
                      ),
                      decoration: new BoxDecoration(
                          color: Colours.app_main,
                          borderRadius: BorderRadius.only(
                              bottomRight: Radius.circular(AppSize.sp(18))))),
                )
              ],
            ),
          ],
        ),
        AppSize.height(310),
        MediaQuery.of(context).size.width * .8);
  }
}
